package com.itheima.controller;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resources;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itheima.po.User;
import com.itheima.utils.JsonResult;
import com.itheima.utils.MybatisUtils;
@Controller
public class UserController {
	@RequestMapping("/getUserList")
	@ResponseBody
	public JsonResult getUserList(Integer page,Integer count) throws IOException {
		SqlSession ss = MybatisUtils.getSession();
		Map<String, Object> map = new HashMap<String, Object>();
		count = count == null ? 5 : count;
		int offset = page == null ? 0 : (page-1)*count;
		map.put("offset", offset);
		map.put("count",count);
		List<User> list = ss.selectList("findUserList", map);
		int total = ss.selectOne("findUserListCount");
		int total_page = (int) Math.ceil(total/(double)count);
		return JsonResult.build(1, list, "OK")
						.put("total_item", total)
						.put("total_page", total_page)
						.put("current_page", page);
	}

}
